package com.cy.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.cy.JsonResult.JsonResult;
import com.cy.VO.ImageVO;
import com.cy.service.ImageService;



@RestController
@RequestMapping("image")
public class ImageController {
	@Autowired
	private ImageService imageService;
	
	/**
	 * 文件上传入门案例
	 * 1.url:/file
	 * 2.请求参数: fileImage
	 * 返回值: 返回字符串
	 * 
	 * 文件上传具体步骤
	 * 1.获取文件名称
	 * 2.指定文件路径
	 * 3.实现文件上传
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/imgFile")
	public JsonResult file(MultipartFile file,HttpServletRequest request) 
			throws IllegalStateException, IOException {
		
		ImageVO img = imageService.upload(file,request);
		System.out.println(img);
		
		
		
		//1.动态获取文件名称
//		String fileName = fileImage.getOriginalFilename();
//		//2.指定文件路径
//		String localDir = "E:/JT_IMAGE";
//		//3.判断文件目录是否存在
//		File dirFile = new File(localDir);
//		if(!dirFile.exists()) {
//			dirFile.mkdirs();	//创建多级目录
//		}
//		//4.实现文件上传	E:/JT_IMAGE/a.jpg
//		String filePath = localDir + "/" + fileName;
//		fileImage.transferTo(new File(filePath));
		return new JsonResult(img,"图片上传成功");
	}
	

}
